package com.cnspt.controllers;

import com.cnspt.models.Users;
import com.cnspt.repository.RedisRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private RedisRepositoryImpl redisRepository;

    @RequestMapping("/addUser")
    public int addUser() {
        Users entity = new Users();
        for(int i=0; i<10; i++) {
            entity.setId(i);
            entity.setName("郭宝宝");
            entity.setMobile("17647369293"+i);
            entity.setSex(2);
            redisRepository.save(entity);
        }
        Map<Integer, Users> lists = redisRepository.findAll();
        System.out.println(lists.size());
        return lists.size();
    }

    @RequestMapping("/getAll")
    public String getAll() {
        String result = "";
        Map<Integer, Users> lists = redisRepository.findAll();
        for (Users user : lists.values()) {
            result += user.toString()+"<br>";
        }
        return result;
    }

    @RequestMapping("/delete")
    public int delete() {
        redisRepository.delete(1);
        Map<Integer, Users> lists = redisRepository.findAll();
        System.out.println(lists.size());
        return lists.size();
    }

    @RequestMapping("/update")
    public int update() {
        Users entity = new Users();
        entity.setId(9);
        entity.setName("郭宝宝1");
        entity.setMobile("15012345678");
        entity.setSex(2);
        redisRepository.update(entity);
        Map<Integer, Users> lists = redisRepository.findAll();
        System.out.println(lists.size());
        return lists.size();
    }

    @RequestMapping("/findUsers")
    public Users findUsers() {
        Users user = redisRepository.find(0);
        return user;
    }
}
